package cn.zmit.kuxi.holder;

import java.util.HashMap;
import java.util.Map;

import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.functions.holder.ViewHolderBase;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.activity.NowJoinActivity;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.entity.GoodsEnity;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.interfaces.OnListItemSelectListener;
import cn.zmit.kuxi.request.RequestTask;

/** * @author chenxu @parameter * @return */
public class SearchHolder extends ViewHolderBase<GoodsEnity> {
	private ImageView goods_pic;
	private TextView title, goodsName, allNeed, join_count;
	private ProgressBar progressBar;
	private Button joinCarts, nowJoin;
	private Context context;
	private RelativeLayout rlRoot;
	@SuppressLint("InflateParams")
	public static OnListItemSelectListener<GoodsEnity>onlistItemSelect;
	private String userid;
	@SuppressLint("InflateParams")
	@Override
	public View createView(LayoutInflater layoutInflater) {
		context = layoutInflater.getContext();
		View view = layoutInflater.inflate(R.layout.item_goods, null);
		goods_pic = (ImageView) view.findViewById(R.id.image_item_goods);
		title = (TextView) view.findViewById(R.id.tv_goods_title);
		goodsName = (TextView) view.findViewById(R.id.tv_goods_detail);
		allNeed = (TextView) view.findViewById(R.id.tv_goods_join);
		// join_count=(TextView)view.findViewById(R.id.tv_goods_title);
		progressBar = (ProgressBar) view.findViewById(R.id.progressBar_goods);
		joinCarts = (Button) view.findViewById(R.id.btn_shopping_carts);
		nowJoin = (Button) view.findViewById(R.id.Btn_join);
		rlRoot=(RelativeLayout)view.findViewById(R.id.rl_root);
		userid=PreferenceHelper.readString(context, SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID, "");
		return view;
	}

	@Override
	public void showData(final int position, final GoodsEnity itemData) {
		System.out.println("position===="+position);
		if (itemData.getUrl() != null && itemData.getUrl().length() > 0) {
			ImageDisplayer.getInstance().dispalyImageWithRadius(Url.IMAGE_PICTURES + itemData.getUrl(), goods_pic, 0);
		} else {
			goods_pic.setImageResource(R.drawable.ic_launcher);
		}
		title.setText("第" + itemData.getTitle() + "期");
		goodsName.setText(itemData.getGoodsName());
		allNeed.setText("总需人数：" + itemData.getAllNeed() + "     " + "已参与人数：" + itemData.getJoinCount());
		
		double all = Integer.parseInt(itemData.getAllNeed());
		double join = Integer.parseInt(itemData.getJoinCount());
		final double result = ((join / all) * 100);
		progressBar.setMax(100);
		progressBar.setProgress((int) (result));
		String goodsId=itemData.getGoodsId();
		String gp_id=itemData.getGp_id();
		joinCarts.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Map<String, Object>map=new HashMap<String,Object>();
				map.put("user_id", userid);
				map.put("gp_id", itemData.getGp_id());
				map.put("number", "1");
				RequestTask.getInstance().doAddCarts((Activity) context, map,new onRequestListener());
			}
		});
		rlRoot.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (onlistItemSelect!=null) {
					onlistItemSelect.onlistItemSelect(position, itemData);
				}
			}
		});
		nowJoin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				Bundle bundle=new Bundle();
				bundle.putString("flag", "搜索页面跳转");
				bundle.putString("title", itemData.getTitle());
				bundle.putString("goodsName", itemData.getGoodsName());
				bundle.putDouble("progress", result);
				bundle.putString("allNeed", itemData.getAllNeed());
				bundle.putString("joinCount", itemData.getJoinCount());
				bundle.putString("gp_id",itemData.getGp_id());
				intent.putExtras(bundle);
				intent.setClass(context, NowJoinActivity.class);
				context.startActivity(intent);
			}
		});
	}
	private class onRequestListener extends OnRequestListenerAdapter<Object>{
		@Override
		public void onDone(XDroidRequest<?> request, String response, DataType dataType) {
			// TODO Auto-generated method stub
			super.onDone(request, response, dataType);
			JsonData jsonData=JsonData.create(response);
			String data=jsonData.optString("code");
			if (data.equals("1")) {
//				ToastUtils.show(context, jsonData.optString("message"));
			}else {
//				ToastUtils.show(context, jsonData.optString("message"));
			}
		}
	}
	/**
	 * 设置回调
	 * 
	 */
public static void setOnListItemSelectListener(OnListItemSelectListener<GoodsEnity>onListItemSelectListener){
	SearchHolder.onlistItemSelect=onListItemSelectListener;
}
}
